﻿using Models;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace DAL
{
    public class D_Group
    {
        public List<Stu_group> getlist()
        {
            var list = Sugar_helper.Db.Queryable<Stu_group>().OrderBy(s_g=>s_g.teacher_id).ToList();
            return list;
        }

        public bool save_List(List<Stu_group> groups)
        {
            var count = Sugar_helper.Db.Storageable<Stu_group>(groups).ExecuteCommand();
            return count == groups.Count ? true : false;
        }

        public Stu_group GetGroup(Student student)
        {
            if (student.group_id == -1) 
                return null;

            var group = Sugar_helper.Db.Queryable<Stu_group>().Where(stu=>stu.group_id == student.group_id).ToList().First();
            return group;
        }

        public int Add(Models.Stu_group stu_Group)
        {
            int itpk = Sugar_helper.Db.Insertable<Stu_group>(stu_Group).ExecuteReturnIdentity();
            Models.g_data g_data = new Models.g_data();
            g_data.g_id = itpk;
            Sugar_helper.Db.Insertable(g_data).ExecuteCommand();
            return itpk;
        }

        public bool update(Stu_group stu_Group)
        {
            var count = Sugar_helper.Db.Updateable<Stu_group>(stu_Group).ExecuteCommand();
            return count != 0 ? true : false;
        }

        public bool release(Stu_group stu_Group)
        {
            var count = Sugar_helper.Db.Deleteable<Stu_group>(stu_Group).ExecuteCommand();
            return count != 0 ? true : false;
        }
    }
}
